﻿Imports System.Data.SqlClient

Public Class XeDAO

    'Hàm lấy giá trị trả ra của hàm _LayDanhSachXe() trong class DataProvider
    Public Function _LayDsXe() As DataTable
        Dim _strSql As String = "usp_LayDsXe"
        Return DataProvider.LoadTable(_strSql)
    End Function

    Public Shared Function NhapXe(ByVal xe As XeDTO) As String

        Dim cmd As New SqlCommand("usp_NhapXe", DataProvider.GetCn())
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add("@TenXe", SqlDbType.NVarChar)
        cmd.Parameters.Add("@MaLoaiXe", SqlDbType.VarChar)
        cmd.Parameters.Add("@MaNSX", SqlDbType.VarChar)
        cmd.Parameters.Add("@SoLuong", SqlDbType.Int)
        cmd.Parameters.Add("@DonGia", SqlDbType.Money)
        cmd.Parameters.Add("@MaXe", SqlDbType.VarChar).Direction = ParameterDirection.Output

        cmd.Parameters("@MaXe").Size = 10
        cmd.Parameters("@TenXe").Value = xe.m_strTenXe
        cmd.Parameters("@MaLoaiXe").Value = xe.m_strMaLoaiXe
        cmd.Parameters("@MaNSX").Value = xe.m_strMaNSX
        cmd.Parameters("@SoLuong").Value = xe.m_iSoLuong
        cmd.Parameters("@DonGia").Value = xe.m_dDonGia

        cmd.ExecuteNonQuery()
        DataProvider.CloseCn()

        Return cmd.Parameters("@MaXe").Value.ToString()

    End Function

    Public Shared Function CapNhatXe(ByVal xe As XeDTO) As Integer

        Dim cmd As New SqlCommand("usp_CapNhatXe", DataProvider.GetCn())
        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add("@MaXe", SqlDbType.VarChar)
        cmd.Parameters.Add("@TenXe", SqlDbType.NVarChar)
        cmd.Parameters.Add("@SoLuong", SqlDbType.Int)
        cmd.Parameters.Add("@DonGia", SqlDbType.Money)
        cmd.Parameters.Add("@out", SqlDbType.Int).Direction = ParameterDirection.Output

        cmd.Parameters("@MaXe").Value = xe.m_strMaXe
        cmd.Parameters("@TenXe").Value = xe.m_strTenXe
        cmd.Parameters("@SoLuong").Value = xe.m_iSoLuong
        cmd.Parameters("@DonGia").Value = xe.m_dDonGia

        cmd.ExecuteNonQuery()
        DataProvider.CloseCn()

        Return Integer.Parse(cmd.Parameters("@out").Value.ToString())

    End Function

    Public Shared Function TimKiemXe(ByVal _strTenXe As String, ByVal _strLoaiXe As String, ByVal _strNSX As String, ByVal _dGiaTu As Double, ByVal _dGiaDen As Double) As DataTable

        Dim da As New SqlDataAdapter("usp_TimKiemXe", DataProvider.GetCn())
        da.SelectCommand.CommandType = CommandType.StoredProcedure

        da.SelectCommand.Parameters.Add("@TenXe", SqlDbType.NVarChar)
        da.SelectCommand.Parameters.Add("@LoaiXe", SqlDbType.VarChar)
        da.SelectCommand.Parameters.Add("@NSX", SqlDbType.VarChar)
        da.SelectCommand.Parameters.Add("@GiaTu", SqlDbType.Money)
        da.SelectCommand.Parameters.Add("@GiaDen", SqlDbType.Money)

        da.SelectCommand.Parameters("@TenXe").Value = _strTenXe
        da.SelectCommand.Parameters("@LoaiXe").Value = _strLoaiXe
        da.SelectCommand.Parameters("@NSX").Value = _strNSX
        da.SelectCommand.Parameters("@GiaTu").Value = _dGiaTu
        da.SelectCommand.Parameters("@GiaDen").Value = _dGiaDen

        Dim dt As New DataTable()
        da.Fill(dt)
        Return dt

    End Function

End Class
